package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-01-27
 * Time: 10:25
 */

// 创建一个泛型类，求一个数组的最大值
class Alg<T extends Comparable<T>> {   //一定实现了比较接口
    public T FindArrayMaxValue(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {   // 引用类型 Object 不能比较大小
                max = array[i];
            }
        }
        return max;
    }

}

// 类中实现一个泛型方法
class Alg2 {
    public <T extends Comparable<T>> T FindValueMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (max.compareTo(array[i]) < 0) {
                max = array[i];
            }
        }
        return max;
    }
}

public class Test {
    public static void main(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] integers = {1, 2, 3, 4, 5, 6, 7};

        Integer ret = alg.FindArrayMaxValue(integers);
        System.out.println(ret);

        Alg2 alg2 = new Alg2();
        System.out.println(alg2.FindValueMax(integers));

    }
}
